	NAME	ZXLOAD
	TITLE	ZXLD  VER 2.1  9/80  RMK
;
; THIS ROUTINE IS A RELOCATING LOADER FOR ZX65.
; WHEN LINKED WITH ZX65R V 2.1, THIS LOADER ALLOWS THAT
; PROGRAM TO ACCOMODATE ANY SIZE CPM SYSTEM
;
	GLOBAL	RELBAS
	PSECT	ABS
;
OFFSET	EQU	0F0FDH
LNGTH	EQU	0F00H
;
; FIND ABSOLUTE BASE FOR ZX65
;
	ORG	100H		; FOR CPM
LDE	LD	SP,LDE
	LD	HL,(1)		; WS VECTOR BIOS+3
	LD	DE,OFFSET	; CMP OFFSET
	ADD	HL,DE
	PUSH	HL		; SAVE ABSBAS
	CCF
	LD	DE,RELBAS
	PUSH	DE		; SAVE RELBAS
	SBC	HL,DE
	EX	DE,HL		; DE=XFRBAS
;
; ADJUST ALL INSTRUCTIONS TO ABSBAS
;
AREL	LD	IX,REL2		; 2-BYTE VALUES
ALP	LD	IY,RELBAS
	CALL	IXFER
	JR	NZ,ALP-$
;
BREL	LD	IX,REL3		; 3-BYTE INSTRS
BLP	LD	IY,RELBAS
	INC	IY
	CALL	IXFER
	JR	NZ,BLP-$
;
CREL	LD	IX,REL4		; 4-BYTE INSTRS
CLP	LD	IY,RELBAS
	INC	IY
	INC	IY
	CALL	IXFER
	JR	NZ,CLP-$
;
; MOVE PGM TO ABSBAS AND JUMP TO IT
;
	POP	HL		; RETRV RELBAS
	POP	DE		; RETRIEVE ABSBAS
	PUSH	DE
	LD	BC,LNGTH
	LDIR
	RET
;
IXFER	LD	B,(IX+1)
	LD	C,(IX+0)	; BC=PGM OFFSET
	LD	A,C
	DEC	A
	RET	Z
	ADD	IY,BC		; IY = INSTRUCTION
	LD	H,(IY+1)
	LD	L,(IY+0)	; HL = REL TGT ADDR
	ADD	HL,DE		; HL = ABS TGT ADDR
	LD	(IY+1),H
	LD	(IY+0),L
	INC	IX
	INC	IX
	RET
;
;
; RELOCATION TABLES FOR ZX65
; **** NOTE **** IF YOU MAKE ANY CHANGES IN ZX65R THAT
; AFFECT THE LOCATION OF ANY NON-RELOCATABLE INSTRUCTION
; THIS LOADER WILL NO LONGER WORK!!!
;
REL2	DEFW	0D62H		; 2-BYTE LABELS
	DEFW	0D64H
	DEFW	0D66H
	DEFW	0D68H
	DEFW	0D6AH
	DEFW	0D6CH
	DEFW	0D6EH
	DEFW	0D70H
	DEFW	0D72H
	DEFW	0D74H
	DEFW	0D76H
	DEFW	0D78H
	DEFW	0D7AH
	DEFW	0D7CH
	DEFW	0D7EH
	DEFW	0D80H
	DEFW	0D82H
	DEFW	0D84H
	DEFW	0D86H
	DEFW	0D88H
	DEFW	0D8AH
	DEFW	0D8CH
	DEFW	0D8EH
	DEFW	0D90H
	DEFW	0D92H
	DEFW	0D94H
	DEFW	0D96H
	DEFW	0D98H
	DEFW	0D9AH
	DEFW	0D9CH
	DEFW	0D9EH
	DEFW	0DA0H
	DEFW	0DA2H
	DEFW	0DA4H
	DEFW	0DA6H
	DEFW	0DA8H
	DEFW	0DAAH
	DEFW	0DACH
	DEFW	0001
;
; THREE-BYTE INSTRUCTIONS
;
REL3	DEFW	0000H
	DEFW	0005H
	DEFW	0008H
	DEFW	000FH
	DEFW	0012H
	DEFW	0015H
	DEFW	0019H
	DEFW	001CH
	DEFW	001FH
	DEFW	0024H
	DEFW	0029H
	DEFW	0036H
	DEFW	003BH
	DEFW	004CH
	DEFW	0051H
	DEFW	0056H
	DEFW	005BH
	DEFW	0060H
	DEFW	0065H
	DEFW	006AH
	DEFW	0073H
	DEFW	0076H
	DEFW	007EH
	DEFW	0081H
	DEFW	0084H
	DEFW	0087H
	DEFW	008DH
	DEFW	009CH
	DEFW	009FH
	DEFW	00A6H
	DEFW	00B5H
	DEFW	00BAH
	DEFW	00C1H
	DEFW	00C4H
	DEFW	00C9H
	DEFW	00D0H
	DEFW	00D7H
	DEFW	00DEH
	DEFW	00E1H
	DEFW	00E4H
	DEFW	00F3H
	DEFW	00F6H
	DEFW	00FCH
	DEFW	00FFH
	DEFW	0102H
	DEFW	0114H
	DEFW	011BH
	DEFW	0122H
	DEFW	0125H
	DEFW	0153H
	DEFW	016BH
	DEFW	0172H
	DEFW	0178H
	DEFW	017FH
	DEFW	0186H
	DEFW	018EH
	DEFW	0193H
	DEFW	0196H
	DEFW	0199H
	DEFW	019EH
	DEFW	01A3H
	DEFW	01ADH
	DEFW	01B2H
	DEFW	01B7H
	DEFW	01BCH
	DEFW	01BFH
	DEFW	01C9H
	DEFW	01CEH
	DEFW	01D1H
	DEFW	01DDH
	DEFW	01E2H
	DEFW	01E5H
	DEFW	01EBH
	DEFW	01F0H
	DEFW	01F3H
	DEFW	01FAH
	DEFW	0205H
	DEFW	0209H
	DEFW	021EH
	DEFW	0221H
	DEFW	0227H
	DEFW	025FH
	DEFW	0266H
	DEFW	026BH
	DEFW	0271H
	DEFW	027EH
	DEFW	028BH
	DEFW	0291H
	DEFW	0296H
	DEFW	0299H
	DEFW	02A3H
	DEFW	02A9H
	DEFW	02ACH
	DEFW	02B7H
	DEFW	02BDH
	DEFW	02C0H
	DEFW	02CDH
	DEFW	02E8H
	DEFW	0313H
	DEFW	031EH
	DEFW	0332H
	DEFW	0339H
	DEFW	0364H
	DEFW	036BH
	DEFW	0371H
	DEFW	0374H
	DEFW	037EH
	DEFW	038CH
	DEFW	0390H
	DEFW	0393H
	DEFW	0399H
	DEFW	039CH
	DEFW	03A2H
	DEFW	03A5H
	DEFW	03ABH
	DEFW	03B1H
	DEFW	03B4H
	DEFW	03E3H
	DEFW	0415H
	DEFW	0418H
	DEFW	042EH
	DEFW	0435H
	DEFW	0438H
	DEFW	043DH
	DEFW	0440H
	DEFW	044AH
	DEFW	044DH
	DEFW	0451H
	DEFW	0454H
	DEFW	045EH
	DEFW	046FH
	DEFW	0476H
	DEFW	047BH
	DEFW	047EH
	DEFW	0484H
	DEFW	0487H
	DEFW	048BH
	DEFW	048EH
	DEFW	0498H
	DEFW	049EH
	DEFW	04A4H
	DEFW	04AAH
	DEFW	04B4H
	DEFW	04BAH
	DEFW	04C0H
	DEFW	04C6H
	DEFW	04D0H
	DEFW	04D4H
	DEFW	04D7H
	DEFW	04E1H
	DEFW	04E5H
	DEFW	04E8H
	DEFW	04EEH
	DEFW	04F1H
	DEFW	04F9H
	DEFW	04FEH
	DEFW	0504H
	DEFW	0514H
	DEFW	051CH
	DEFW	052FH
	DEFW	0542H
	DEFW	054EH
	DEFW	0555H
	DEFW	0561H
	DEFW	0568H
	DEFW	0574H
	DEFW	057FH
	DEFW	0587H
	DEFW	0598H
	DEFW	05ABH
	DEFW	05B1H
	DEFW	05B7H
	DEFW	05BDH
	DEFW	05C3H
	DEFW	05CDH
	DEFW	05D7H
	DEFW	065AH
	DEFW	06E9H
	DEFW	06ECH
	DEFW	06EFH
	DEFW	06F3H
	DEFW	06F6H
	DEFW	06F9H
	DEFW	06FCH
	DEFW	0715H
	DEFW	0718H
	DEFW	071BH
	DEFW	0722H
	DEFW	0725H
	DEFW	072CH
	DEFW	073DH
	DEFW	074BH
	DEFW	074EH
	DEFW	0751H
	DEFW	0755H
	DEFW	0758H
	DEFW	075BH
	DEFW	075FH
	DEFW	0762H
	DEFW	0765H
	DEFW	076FH
	DEFW	0778H
	DEFW	077BH
	DEFW	0782H
	DEFW	0791H
	DEFW	0798H
	DEFW	079DH
	DEFW	07A3H
	DEFW	07A8H
	DEFW	07ABH
	DEFW	07B8H
	DEFW	07C3H
	DEFW	07CCH
	DEFW	07D5H
	DEFW	07D8H
	DEFW	07DEH
	DEFW	07EBH
	DEFW	07F2H
	DEFW	07F5H
	DEFW	07F8H
	DEFW	07FBH
	DEFW	0804H
	DEFW	0807H
	DEFW	080AH
	DEFW	080DH
	DEFW	0813H
	DEFW	0816H
	DEFW	081BH
	DEFW	081EH
	DEFW	0821H
	DEFW	082EH
	DEFW	0831H
	DEFW	0834H
	DEFW	0837H
	DEFW	0840H
	DEFW	0843H
	DEFW	0846H
	DEFW	0849H
	DEFW	085FH
	DEFW	0862H
	DEFW	08A7H
	DEFW	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